import axios from "axios";
export const CHANGE_INPUT_VALUE = "change_input_value";
export const BUTTON_CLICK = "button_click";
export const DELETE_ITEM = "delete_item";
export const INIT_ITEM = "init_item";
export const GET_INIT_LIST = "get_init_list";

// 声明的action
export const getInputChangeAction = (value) => ({
  type: CHANGE_INPUT_VALUE,
  value,
});
export const getBtnClickAction = () => ({
  type: BUTTON_CLICK,
});
export const getDeleteItemAction = (index) => ({
  type: DELETE_ITEM,
  index,
});
export const InitStateAction = (list) => ({
  type: INIT_ITEM,
  list,
});

// 使用thunk 以后 可以返回一个函数
export const getTodoList = () => {
  return (dispatch) => {
    axios
      .get("api/todolist")
      .then((res) => {
        const data = res.data;
        console.log("异步数据", data);
        // 获取到异步请求结果 放入 仓库中
        const action = InitStateAction(data);
        console.log("dispatch方法", dispatch);
        dispatch(action);
        // const action = InitStateAction(res.data)
        // store.dispatch(action)
      })
      .catch((err) => {
        console.log(err);
      });
  };
};

// 使用saga 进行任务派发
export const getInitList = () => ({
  type: GET_INIT_LIST,
});
